<?php

namespace app\common\model\user;

use think\Model;


class Jz extends Model
{





    // 表名
    protected $name = 'user_jz';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'sex_text',
        'age_text'
    ];



    public function getSexList()
    {
        return ['male' => __('Sex male'), 'female' => __('Sex female')];
    }

    public function getAgeList()
    {
        return ['0' => __('Age 0'), '1' => __('Age 1')];
    }


    public function getSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
        $list = $this->getSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAgeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['age']) ? $data['age'] : '');
        $list = $this->getAgeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    //更新就诊人
    public static function updateDefault($data)
    {
        $model = new self();
        self::update(['is_default' => 0], ['user_id' => $data['user_id']]);
        if ($data['jz_id']) {
            $model->save(['is_default' => 1], ['id' => $data['jz_id']]);
        } else {
            $model->allowField(true)->save($data);
        }
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
